<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use app\index\validate\Valid;

class Syspenapproval extends Common
{

	//列表
    public function index()
    {
        $zone = $this->request->get('zone');
        $zoning_head = $this->request->get('zoning_head');
        $zoning = $this->request->get('zoning');
        $provice = $this->request->get('provice');
        $provice_head = $this->request->get('provice_head');
        $city = $this->request->get('city');
        $city_head = $this->request->get('city_head');
        $jxs_sn = $this->request->get('jxs_sn');
        $jxs_title = $this->request->get('jxs_title');
        $systemName = $this->request->get('systemName');
        $systemNo = $this->request->get('systemNo');
        $stuff = $this->request->get('stuff');
        $appType = $this->request->get('appType');

        $this->assign(array('zone'=>$zone));
        $this->assign(array('zoning_head'=>$zoning_head));
        $this->assign(array('zoning'=>$zoning));
        $this->assign(array('provice'=>$provice));
        $this->assign(array('city'=>$city));
        $this->assign(array('provice_head'=>$provice_head));
        $this->assign(array('city_head'=>$city_head));
        $this->assign(array('jxs_sn'=>$jxs_sn));
        $this->assign(array('jxs_title'=>$jxs_title));
        $this->assign(array('systemName'=>$systemName));
        $this->assign(array('systemNo'=>$systemNo));
        $this->assign(array('stuff'=>$stuff));
        $this->assign(array('appType'=>$appType));

        $where = [];
        if($zone)
            $where['t2.zoning'] = ['exp',"in(select id from cg_market_zoning where title like '%".$zone."%')"];
        if($zoning_head)
            $where['t2.zoning'] = ['exp',"in(select t.id from cg_market_zoning t join employee t2 on t2.id=t.head where t2.em_name like '%".$zoning_head."%')"];
        if($provice)
            $where['t2.provice'] = ['exp',"in(select id from cg_market_provice where title like '%".$provice."%')"];
        if($provice_head)
            $where['t2.provice'] = ['exp',"in(select t.id from cg_market_provice t join employee t2 on t2.id=t.head where t2.em_name like '%".$provice_head."%')"];
        if($city)
            $where['t2.city'] = ['exp',"in(select id from cg_market_city where title like '%".$city."%')"];
        if($city_head)
            $where['t2.city'] = ['exp',"in(select t.id from cg_market_city t join employee t2 on t2.id=t.head where t2.em_name like '%".$city_head."%')"];
        if($jxs_sn)
            $where['t2.jxs'] = ['exp',"in(select id from cg_jxs where sn like '%".$jxs_sn."%')"];
        if($jxs_title)
            $where['t2.jxs'] = ['exp',"in(select id from cg_jxs where title like '%".$jxs_title."%')"];
        if($systemName){
            $where['t2.title'] = ['like','%'.$systemName.'%'];
        }
        if($systemNo){
            $where['t2.sn'] = ['like','%'.$systemNo.'%'];
        }
        if($appType!='quanbu'){
            $where['t.appType'] = ['like','%'.$appType.'%'];
        }
        if($stuff){
            //根据模糊查询规则把admin对应的id全部查询出来
            $ids = Db::query("select id from employee where em_name like '%{$stuff}%'");
            $str = [];
            foreach($ids as $k=>$v){
                if($k==0)
                    $str[] = " like '%\"$v[id]\"%' ";
                else
                    $str[] = " stuff like '%\"$v[id]\"%' ";
            }
            $str = implode('or',$str);

            $where['t2.stuff'] = array('exp',$str);
        }

        $count = Db::table('cg_system_approval t')->join('cg_system t2','t2.id=t.system_id')->where($where)->count();
        $data = Db::table('cg_system_approval t')->field('*,t2.id system_id')->join('cg_system t2','t2.id=t.system_id')->order("t.id desc")->where($where)->paginate(10,$count);
//        echo Db::table('cg_system')->getLastSql();
        $page = $data->render();
        $data = $data->items();
         $maxpage=ceil($count/10);
        foreach($data as $k=>$v){
            $data[$k]['cusername'] = Db::table('users')->where(array('id'=>$v['cuser']))->value('login_name');
            $data[$k]['jxs_title'] = Db::table('cg_jxs')->where(array('id'=>$v['jxs']))->value('title');
            $data[$k]['jxs_sn'] = Db::table('cg_jxs')->where(array('id'=>$v['jxs']))->value('sn');
            $data[$k]['zoning_title'] = Db::table('cg_market_zoning')->where(array('id'=>$v['zoning']))->value('title');
            $data[$k]['provice_title'] = Db::table('cg_market_provice')->where(array('id'=>$v['provice']))->value('title');
            $data[$k]['city_title'] = Db::table('cg_market_city')->where(array('id'=>$v['city']))->value('title');
            $data[$k]['ctime'] = date('Y-m-d H:i',$v['ctime']);
        }

        $this->assign('data',$data);
        $this->assign('page',$page);
        $this->assign('maxpage',$maxpage);
        $this->assign('count',$count);
        return $this->fetch('');
    }


    /**
     * 查询经销商的部分信息
     */
    public function findJxs(){
        $id = $this->request->post('id');
        $data = Db::table('cg_jxs')->where('id',$id)->field('sn,zoning,provice,city')->find();
        $data['city'] = Db::table('cg_market_city t')->join('employee t2','t2.id= t.head')->where('t.id',$data['city'])->value('t2.em_name');
        $data['provice'] = Db::table('cg_market_provice t')->join('employee t2','t2.id= t.head')->where('t.id',$data['provice'])->value('t2.em_name');
        $data['zoning'] = Db::table('cg_market_zoning t')->join('employee t2','t2.id= t.head')->where('t.id',$data['zoning'])->value('t2.em_name');
        echo json_encode($data);die;
    }

    public function twoCType(){
        $id = $this->request->get('id');
        //查询二级渠道类型
        $channel_type2 = Db::table('cg_channel_types2')->where('pid',$id)->field('title,id')->select();
        echo '<option value="">请选择</option>';
        foreach($channel_type2 as $k=>$v) {
            echo '<option value="'.$v['id'].'">'.$v['title'].'</option>';
        }
    }

    public function selectJXS(){
        return $this->fetch();
    }

    //返回分区经销商树数据
    public function zonetreeajax(){
        $data = Db::table('cg_market_zoning')->where('isdel',0)->field('id,title')->select();//查分区
        $data2 = [];
        foreach($data as $k=>$v){
            $temp = [];
            $temp['name'] = $v['title'];
            //查询省份
            $t = Db::table('cg_market_provice')->where(array('zoning'=>$v['id']))->field('id,title')->select();//查省份
            $data3 = [];
            foreach($t as $kk=>$vv){
                $temp2['name'] = $vv['title'];
                //查城市
                $tt = Db::table('cg_market_city')->where(array('provice'=>$vv['id']))->field('id,title')->select();//查城市
                $data4 = [];
                foreach($tt as $k3=>$v3){
                    $temp3['name'] = $v3['title'];
                    //查城市
                    $t3 = Db::table('cg_jxs')->where(array('city'=>$v3['id']))->field('id,title name')->select();//查经销商
                    $temp3['children'] = $t3;
                    $temp3["chkDisabled"]=true;
                    $temp3["nocheck"]=true;
                    $data4[] = $temp3;
                }
                $temp2['children'] = $data4;
                $temp2["chkDisabled"]=true;
                $temp2["nocheck"]=true;
                $data3[] = $temp2;
            }
            $temp['children'] = $data3;
            $temp["chkDisabled"]=true;
            $temp["nocheck"]=true;
            $data2[] = $temp;
        }
        echo json_encode($data2);
    }

    ////查询所有省份
    public function provicejson(){

        $data = Db::table('employee')->field('em_name,id')->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }

    //查询所有经销商
    public function jxsjson(){
        //查询所有省份
        $data = Db::table('cg_jxs')->field('title em_name,id')->select();

        echo json_encode(['message'=>'ok','value'=>$data,'code'=>'200','redirect'=>'']);
    }



    public function delete(){
        $id = $this->request->param('id');
        if(!$id){
            echo  json_encode([
                "success"=>false,
                "msg"=>"参数错误"
            ]);die;
        }
        $data = [];
        $data['isdel'] = 1;
        $bool = Db::table('cg_system')->where(array('id'=>$id))->update($data);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"删除成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"删除失败"
            ]);die;
        }
    }


    /**
     * 查看系统明细
     * @return mixed
     */
    public function viewDetail(){

        $appid = $this->request->param('id');
        $id = $this->request->param('system_id');


        $data = Db::table('cg_system')->where('id',$id)->find();
        if($data['stuff']){
            $data['stuff'] = unserialize($data['stuff']);
            foreach($data['stuff'] as $k=>$v){
                $data['stuff'][$k] = Db::table('employee')->where('id',$v)->value('em_name');
            }
            $data['stuff'] = implode(',',$data['stuff']);
        }
        $data['cusType'] = Db::table('cg_customertype')->where('id',$data['cusType'])->value('title');
        $data['oneCType'] = Db::table('cg_channel_types1')->where('id',$data['oneCType'])->value('title');
        $data['twoCType'] = Db::table('cg_channel_types2')->where('id',$data['twoCType'])->value('title');
        //查询系统所属经销商
        $jxs = Db::table('cg_system_jxs')->where('system_id',$id)->order('`default` desc')->select();
        foreach($jxs as $k=>$v){
            $jxs[$k]['title'] = Db::table('cg_jxs')->field('title')->where('id',$v['jxs_id'])->value('title');
            $jxs[$k]['sn'] = Db::table('cg_jxs')->field('sn')->where('id',$v['jxs_id'])->value('sn');
            $jxs[$k]['zoning_head'] = Db::table('cg_jxs t')->join('cg_market_zoning t2','t2.id=t.zoning')->join('employee t3','t2.head=t3.id')->field('t3.em_name')->where('t.id',$v['jxs_id'])->value('t3.em_name');
            $jxs[$k]['provice_head'] = Db::table('cg_jxs t')->join('cg_market_provice t2','t2.id=t.provice')->join('employee t3','t2.head=t3.id')->field('t3.em_name')->where('t.id',$v['jxs_id'])->value('t3.em_name');
            $jxs[$k]['city_head'] = Db::table('cg_jxs t')->join('cg_market_city t2','t2.id=t.city')->join('employee t3','t2.head=t3.id')->field('t3.em_name')->where('t.id',$v['jxs_id'])->value('t3.em_name');
        }
        $data['jxs'] = $jxs;
        $data['ci'] = Db::table('cg_system_ci')->where('system_id',$id)->select();
        if($data['attach']){
            $attach = unserialize($data['attach']);
            foreach($attach as $k=>$v){
                $filename = Db::table('annex')->where('id',$v)->value('file_name');
                $path =  Db::table('annex')->where('id',$v)->value('path');
                $attach[$k] = DS.'uploads'.DS.$path.DS.$filename;
            }
            $data['attach'] = $attach;
        }

        //查询系统评价信息
        $evaluation = Db::table('cg_system_evaluation')->where(['system_id'=>$id,'cuser'=>Session::get('employee_id')])->find();
        $data['evaluation'] = $evaluation;
        $this->assign('data',$data);
        return $this->fetch();
    }
    /**
     * 审核明细
     * @return mixed
     */
    public function view(){

        $appid = $this->request->param('id');
        $id = $this->request->param('system_id');

        if($_POST){
            $attach = $this->request->post('attach');
            if($attach){
                $attachids = $attach;
                $attach = serialize(explode(',',$attach));
                $bool = Db::table('cg_system')->where('id',$id)->update(['attach'=>$attach]);
                if(isset($attachids)){
                    Db::table('annex')->where(array('id'=>array('in',$attachids)))->update(array('target_id'=>$id,'target_table'=>'cg_system'));
                }
                if(false!==$bool){
                    echo json_encode(['success'=>true,'msg'=>'附件提交成功']);
                }else{
                    echo json_encode(['success'=>false,'msg'=>'附件提交失败']);
                }
            }


            $data = [];
            $data['appType'] = $this->request->post('type');
            $data['appBrief'] = $this->request->post('appBrief');
            $bool = Db::table('cg_system_approval')->where('id',$appid)->update($data);
            if(false !== $bool){
                if($data['appType']==1){
                    //修改系统表的审核通过字段isApp
                    Db::table('cg_system')->where('id',$id)->update(['isApp'=>1]);
                }
                echo  json_encode([
                    "success"=>true,
                    "msg"=>"操作成功"
                ]);die;
            }else{
                echo  json_encode([
                    "success"=>false,
                    "msg"=>"操作失败"
                ]);die;
            }
            die;
        }

        $data = Db::table('cg_system')->where('id',$id)->find();
        if($data['stuff']){
            $data['stuff'] = unserialize($data['stuff']);
            foreach($data['stuff'] as $k=>$v){
                $data['stuff'][$k] = Db::table('employee')->where('id',$v)->value('em_name');
            }
            $data['stuff'] = implode(',',$data['stuff']);
        }
        $data['cusType'] = Db::table('cg_customertype')->where('id',$data['cusType'])->value('title');
        $data['oneCType'] = Db::table('cg_channel_types1')->where('id',$data['oneCType'])->value('title');
        $data['twoCType'] = Db::table('cg_channel_types2')->where('id',$data['twoCType'])->value('title');
        //查询系统所属经销商
        $jxs = Db::table('cg_system_jxs')->where('system_id',$id)->order('`default` desc')->select();
        foreach($jxs as $k=>$v){
            $jxs[$k]['title'] = Db::table('cg_jxs')->field('title')->where('id',$v['jxs_id'])->value('title');
            $jxs[$k]['sn'] = Db::table('cg_jxs')->field('sn')->where('id',$v['jxs_id'])->value('sn');
            $jxs[$k]['zoning_head'] = Db::table('cg_jxs t')->join('cg_market_zoning t2','t2.id=t.zoning')->join('employee t3','t2.head=t3.id')->field('t3.em_name')->where('t.id',$v['jxs_id'])->value('t3.em_name');
            $jxs[$k]['provice_head'] = Db::table('cg_jxs t')->join('cg_market_provice t2','t2.id=t.provice')->join('employee t3','t2.head=t3.id')->field('t3.em_name')->where('t.id',$v['jxs_id'])->value('t3.em_name');
            $jxs[$k]['city_head'] = Db::table('cg_jxs t')->join('cg_market_city t2','t2.id=t.city')->join('employee t3','t2.head=t3.id')->field('t3.em_name')->where('t.id',$v['jxs_id'])->value('t3.em_name');
        }
        $data['jxs'] = $jxs;
        $data['ci'] = Db::table('cg_system_ci')->where('system_id',$id)->select();
        if($data['attach']){
            $attach = unserialize($data['attach']);
            foreach($attach as $k=>$v){
                $filename = Db::table('annex')->where('id',$v)->value('file_name');
                $path =  Db::table('annex')->where('id',$v)->value('path');
                $attach[$k] = DS.'uploads'.DS.$path.DS.$filename;
            }
            $data['attach'] = $attach;
        }

        //查询系统评价信息
        $evaluation = Db::table('cg_system_evaluation')->where(['system_id'=>$id,'cuser'=>Session::get('employee_id')])->find();
        $data['evaluation'] = $evaluation;
        $this->assign('data',$data);
        return $this->fetch();
    }

    /**
     * 获取经销商
     */
    public function getjxses(){
        $id = $this->request->get('id');
        $fenqu = Db::table('cg_jxs')->where(array('city'=>$id))->field('id,title')->select();
        echo '<option value="">请选择</option>';
        foreach($fenqu as $k=>$v) {
            echo '<option value="'.$v['id'].'">'.$v['title'].'</option>';
        }
    }

    /**
     * 提交评价
     */
    public function subEvaluation(){
        $data = [];
        $data['cooIntention'] = $this->request->post('cooIntention');
        $data['cusSize'] = $this->request->post('cusSize');
        $data['cusInfluence'] = $this->request->post('cusInfluence');
        $data['cusCovArea'] = $this->request->post('cusCovArea');
        $data['system_id'] = $this->request->post('system_id');
        $data['cuser'] = Session::get('employee_id');
        $data['ctime'] = time();

        $evaluation = Db::table('cg_system_evaluation')->where(['system_id'=>$data['system_id'],'cuser'=>Session::get('employee_id')])->count();
        if($evaluation>0){
            echo  json_encode([
                "success"=>false,
                "msg"=>"不能重复评价"
            ]);die;
        }
        $bool = Db::table('cg_system_evaluation')->insert($data);
        if(false !== $bool){
            echo  json_encode([
                "success"=>true,
                "msg"=>"评价成功"
            ]);die;
        }else{
            echo  json_encode([
                "success"=>false,
                "msg"=>"评价失败"
            ]);die;
        }
    }

    public function setRemindPeople(){
        return $this->fetch();
    }
}
